//
//  PersonViewController.m
//  Taste
//
//  Created by 徐昊 on 16/9/9.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "PersonViewController.h"
#import "PersonTableViewCell.h"
#import "PersonModel.h"
#import "ZWYNetTool.h"
#import "PersonRes.h"
#import "UITableView+SDAutoTableViewCellHeight.h"
#import "HXMacro.h"
#import "PersonCon.h"
#import "UIView+SDAutoLayout.h"
#import "SelfSizeTool.h"
#import "UIView+Frame.h"
#import "UIImageView+WebCache.h"
#import "NSString+Vali.h"
#import "HomePageDetailViewController.h"

#define kPersonTableViewCellName @"PersonTableViewCell"
@interface PersonViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, strong) PersonModel *model;
@property (nonatomic, strong) NSArray *array;
@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UILabel *headerViewTitle;
@property (weak, nonatomic) IBOutlet UILabel *headerViewPosition;
@property (weak, nonatomic) IBOutlet UILabel *headerViewIntroduce;
@property (weak, nonatomic) IBOutlet UIImageView *headerViewPic;

@end

@implementation PersonViewController

- (NSArray *)array
{
    if (!_array) {
        _array = [NSArray new];
    }
    return _array;
}

- (void)viewWillAppear:(BOOL)animated {
    self.navigationController.navigationBarHidden = YES;
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    
    [_tableView registerNib:[UINib nibWithNibName:@"PersonTableViewCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:kPersonTableViewCellName];
    NSString *string = [NSString stringWithFormat:@"id=%@", _numString];
    NSString *lenthString = [NSString stringWithFormat:@"%ld", string.length];
    NSDictionary *dic = @{@"version" : @"2.50",
                          @"Content-Type" : @"application/x-www-form-urlencoded",
                          @"Content-Length" : lenthString};
    [ZWYNetTool POST:@"http://app.legendzest.cn/index3.php?m=api250&c=master&a=getinfo" andBody:string andBodyStyle:ZWYBodyString andHeader:dic andResponse:ZWYJSON andSuccessBlock:^(id result) {
        _model = [PersonModel modelWithDic:result];
        self.array = _model.res.con;
        [_headerViewPic sd_setImageWithURL:[NSURL URLWithString:[NSString imageWebWithStr:_model.res.pic]] placeholderImage:[UIImage imageNamed:@"IMG_Universal_Placeholder"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
        }];
        _headerViewTitle.text = _model.res.title;
        _headerViewPosition.text = [NSString stringWithFormat:@"%@ / %@ / %@", _model.res.alias, _model.res.position, _model.res.company];
        _headerViewIntroduce.text = _model.res.short_content;
        _headerView.h_height = [SelfSizeTool selfSizeHeightWith:_headerViewTitle.text width:self.view.h_width - 30 font:[UIFont systemFontOfSize:20]] + [SelfSizeTool selfSizeHeightWith:_headerViewPosition.text width:self.view.h_width - 30 font:[UIFont systemFontOfSize:15]] + [SelfSizeTool selfSizeHeightWith:_headerViewIntroduce.text width:self.view.h_width - 30 font:[UIFont systemFontOfSize:15]] + SCREEN_WIDTH + 50;
        _tableView.tableHeaderView = _headerView;
        [_tableView reloadData];
    } andFailureBlock:^(NSError *error) {
        NSLog(@"请求失败");
        
    }];

}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _model.res.con.count;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSInteger num = 0;
    if (_model.res.con.count) {
        num += 1;
    }
//    if (_model.res.restaurant) {
//        num += 1;
//    }
//    if (_model.res.cookbook) {
//        num += 1;
//    }
    return num;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    PersonTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:kPersonTableViewCellName];
    cell.con = _model.res.con[indexPath.row];
    return cell;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return @"相关报道";
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSString *str = [_model.res.con[indexPath.row] title];
    NSString *string = [_model.res.con[indexPath.row] short_content];
    return [SelfSizeTool selfSizeHeightWith:str width:self.view.h_width - 10 font:[UIFont systemFontOfSize:20]] + [SelfSizeTool selfSizeHeightWith:string width:self.view.h_width - 10 font:[UIFont systemFontOfSize:15]] + SCREEN_WIDTH + 30;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    HomePageDetailViewController *vc = [[HomePageDetailViewController alloc] init];
    vc.numString = _model.res.con[indexPath.row].personCon_id;
    [self.navigationController pushViewController:vc animated:YES];
}
- (IBAction)pop:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
